﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using CloudSoft.Workflows;

namespace ERPStore.Exporter.Workflows.Activities
{
	public sealed class SaveBanishedProductList : CodeActivity
	{
		public InArgument<List<Models.BanishedProduct>> BanishedProductList { get; set; }

		protected override void Execute(CodeActivityContext context)
		{
			var list = context.GetValue(this.BanishedProductList);
			var exporterService = context.GetService<Services.ExporterService>();
			var logger = context.GetService<Logging.ILogger>();

			context.ReportProgress("start banished product list");

			if (list == null)
			{
				return;
			}
			context.ReportProgress(list.Count, 0, list.Count, "start product list banishment");
			foreach (var item in list)
			{
				try
				{
					exporterService.SaveBanishedProduct(item);
					context.ReportProgress(string.Format("product {0} / {1} added in banished product list until {2:ddMMyy}", item.ProductId, item.SourceId, item.ReleaseDate));
				}
				catch (Exception ex)
				{
					context.ReportException(ex);
					logger.Error(ex);
					throw;
				}
			}
			context.ReportProgress("end banishment");
		}
	}
}
